require("../util.js");
require("../component/components.js");
require("../component/indexList.js");
require("../component/indexHotlist.js");
require("../component/indexSlide.js");
require("../../jsMt/util.js");
var vm = new Vue({
    el:"#seeIndex",
    data:{
        isIndextop:false,
        indexSlide1:{},//顶部轮播
        categoryList:{},//分类
        recommendList:{},//推荐广告
        recommendList1:{},
        recommendList2:{},
        recommendList3:{},
        enterpriseList:{},
        enterpriseList1:{},
        enterpriseList2:{},//企业广告
        mediaList:{},
        mediaList1:{},
        mediaList2:{},//自媒体广告
        cityList:{},//城市列表
        cityAdvList:{},//城市广告
        traditionList:{},
        traditionList1:{},
        traditionList2:{},//传统广告
        hotAdvList:{
            list:{}
        },//人气广告
        hotAdvList1:[],
        hotAdvList2:[],
        pageNumber:"1",
        staticList:{},//静态广告
        loadFlag:false
    },
    mounted:function(){
        this.getCarouselList();
        this.getCategoryList();
        this.getRecommendList();
        this.getEnterpriseList();
        this.getMediaList();
        this.getCityList();
        this.getCityAdvList();
        this.getTraditionList();
        this.getHotAdvList();
        this.getStaticList();
        lazyload();
    },
    methods:{
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        isActive:function(){
            this.isIndextop=!this.isIndextop;
        },
        //首页顶部轮播图
        getCarouselList:function(){
            var that=this;
            this.ajax({url:"/getCarouselList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.indexSlide1=b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取轮播失败！")
                }
            });
        },
        //分类
        getCategoryList:function(){
            var that=this;
            this.ajax({url:"/getCategoryList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.categoryList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                }else{
                    vm.$refs.prompt.toast("获取分类失败！")
                }
            });
        },
        //获取主页推荐广告列表
        getRecommendList:function(){
            var that=this;
            this.ajax({url:"/getRecommendList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.recommendList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                    that.recommendList1=that.recommendList[0];
                    that.recommendList2=that.recommendList[1];
                    that.recommendList3=that.recommendList.slice(2);
                }else{
                    vm.$refs.prompt.toast("获取推荐广告失败！")
                }
            });
        },
        //获取主页企业广告列表
        getEnterpriseList:function(){
            var that=this;
            this.ajax({url:"/getEnterpriseList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.enterpriseList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                    that.enterpriseList1=that.enterpriseList[0];
                    that.enterpriseList2=that.enterpriseList.slice(1);
                }else{
                    vm.$refs.prompt.toast("获取企业广告失败！")
                }
            });
        },
        //获取主页自媒体广告列表
        getMediaList:function(){
            var that=this;
            this.ajax({url:"/getMediaList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.mediaList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                    that.mediaList1=that.mediaList[0];
                    that.mediaList2=that.mediaList.slice(1);
                }else{
                    vm.$refs.prompt.toast("获取自媒体广告失败！");
                }
            });
        },
        //获取主页城市列表
        getCityList:function(){
            var that=this;
            this.ajax({url:"/getCityList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.cityList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                }else{
                    vm.$refs.prompt.toast("获取城市列表失败！");
                }
            });
        },
        //获取主页城市广告列表
        getCityAdvList:function(){
            var that=this;
            this.ajax({url:"/getCityAdvList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.cityAdvList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                    lazyload();
                }else{
                    vm.$refs.prompt.toast("获取城市广告失败！");
                }
            });
        },
        //获取主页传统广告列表
        getTraditionList:function(){
            var that=this;
            this.ajax({url:"/getTraditionList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.traditionList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                    that.traditionList1=that.traditionList[0];
                    that.traditionList2=that.traditionList.slice(1);
                    lazyload();
                }else{
                    vm.$refs.prompt.toast("获取传统广告失败！");
                }
            });
        },
        //获取主页人气分页列表
        getHotAdvList:function(){
            var that=this;
            var data={
                pageNumber:that.pageNumber
            };
            this.ajax({url:"/getHotAdvList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.hotAdvList=b.json.data;
                    that.hotAdvList1=that.hotAdvList.list;
                    if(that.hotAdvList.pageNumber>1){
                        if(that.hotAdvList.list.length>0) {
                            that.hotAdvList2 = that.hotAdvList2.concat(b.json.data.list);
                        }
                    }else{
                        that.hotAdvList2= that.hotAdvList1;
                    }
                    console.log(that.hotAdvList);
                    vm.loadFlag=false;
                    console.log(that.hotAdvList2);
                    lazyload();
                }else{
                    vm.$refs.prompt.toast("获取人气广告失败！")
                }
            });
        },
        //获取主页静态广告列表
        getStaticList:function(){
            var that=this;
            this.ajax({url:"/getStaticList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.staticList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                    // that.traditionList1=that.traditionList[0];
                    // that.traditionList2=that.traditionList.slice(1);
                    lazyload();
                }else{
                    vm.$refs.prompt.toast("获取静态广告失败！")
                }
            });
        }
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag = vm.hotAdvList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            vm.getHotAdvList();
        }else{
            lazyload();
          vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
// var mySwiper = new Swiper('#index-popularity',{
//     direction: 'vertical',
//     scrollbar: '.swiper-scrollbar',
//     slidesPerView: 'auto',
//     mousewheelControl: true,
//     freeMode: true,
//     onTouchEnd: function(swiper) {
//         var loadFlag = vm.hotAdvList.lastPage;
//         var _viewHeight = document.getElementsByClassName('swiper-wrapper')[0].offsetHeight;
//         var _contentHeight = document.getElementsByClassName('swiper-slide')[0].offsetHeight;
//         console.log(vm.pageNumber+" loadFlag"+loadFlag+";_contentHeight"+_contentHeight);
//         // 上拉加载
//         if(mySwiper.translate <= _viewHeight - _contentHeight - 50 && mySwiper.translate < 0) {
//             if(!loadFlag){
//                 $(".loadtip").html('正在加载...');
//                 //setTimeout(function() {
//                 vm.pageNumber++;
//                 // $(".list-group").append('<li class="list-group-item">我是加载出来的'+oi+'...</li>');
//                 vm.getHotAdvList();
//                 $(".loadtip").html('上拉加载更多...');
//                 mySwiper.update(); // 重新计算高度;
//                 // }, 800);
//             }else{
//                 $(".loadtip").html('没有更多啦！');
//             }
//         }
//         return false;
//     }
// });
function lazyload(){
    $("img.lazy").lazyload();
}